home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3980 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  106 lines

  1. Path: netnews1.apci.com!usenet
  2. From: wardmw@apci.com (Martin Ward)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Multiplication Problem - How to multiply really big numbers, and printing the answers out.
  5. Date: Thu, 01 Feb 1996 07:46:17 GMT
  6. Organization: Air Products Europe
  7. Message-ID: <4eprck$4o6@netnews1.apci.com>
  8. References: <4ep07m$4fn@newsbf02.news.aol.com>
  9. Reply-To: wardmw@apci.com
  10. NNTP-Posting-Host: p1862.apci.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. agent439@aol.com (Agent439) typed:
  14.  
  15.  
  16. >Here is the program
  17. >#include <math.h>
  18. >#include <stdio.h>
  19. >#include <time.h>
  20.  
  21. >#define FALSE 0
  22. >#define TRUE !FALSE
  23.  
  24. >float timer(int reset);
  25. >void main(){
  26. >    
  27. >    char a[100],b[100];
  28. >    int i, j;
  29. >    int x, y, z;
  30. >    int size,truesize;
  31. >    unsigned long pa,pb, c;
  32. >    
  33. >    a==NULL;
  34. >    b==NULL;
  35. >    printf("Enter the Multiplicand:");
  36. >    gets(a);
  37. >    while(a<0){
  38.  
  39. You've defined  a  as a character array, so trying to check it's less than 0
  40. like this will fail.  Try something like:
  41.     while(a[0] == '-') {
  42.  
  43. but you'll need to watch out for leading spaces.
  44.  
  45.  
  46. >        printf("Positive Integers only please!");
  47. >        printf("Enter the Multiplicand:");
  48. >        gets(a);
  49. >    }
  50. >    
  51. >    printf("Enter the Multiplier:");
  52. >    gets(b);
  53. >    while(b<0){
  54. Same problem as  a  above.
  55.  
  56. >        printf("Positive Integers only please!");
  57. >        printf("Enter the Multiplier:");
  58. >        gets(b);
  59. >    }
  60. >    size=0;
  61. >    pa=0;
  62. >    while(a[size] != 0){
  63. >    size++;
  64. >    }
  65. >    size--;
  66. You could replace the above code with
  67.     size = strlen(a);
  68. unless you're banned from using string functions.
  69.  
  70. >    truesize=size;
  71. >    for(size==size;size>=0;size--){
  72. Shouldn't this be:
  73.      for (size = size; size >= 0; size --) {
  74.                ^
  75.                One equals sign.
  76.  
  77. >    pa += (a[size]-48)*(pow(10,(truesize-size))); //change char to integer
  78. >value 
  79. >        }
  80. >    size=0;
  81. >    pb=0;
  82. >    while(b[size] != 0){
  83. >    size++;
  84. >    }
  85. >    size--;
  86. >    truesize=size;
  87. >    for(size==size;size>=0;size--){
  88. >    pb += (b[size]-48)*(pow(10,(truesize-size))); //change char to
  89. >integer value
  90. >        }
  91. >    //timer(TRUE);
  92. >    c=0;
  93. >    c+=(pa);//Start C with the amount in pa
  94. >    c*=(pb);    //Multiply pa by the integer value of pb
  95. >    printf("The product is =======> %i \n",c);
  96.  
  97. I've not tried to compile your code, just had a glance through it, but it
  98. strikes me that here, you're trying to print out the product of two very large
  99. numbers.  These number themselves can't be held in an integer variable, so
  100. there's no way the product can!
  101.  
  102. HTH
  103.  
  104. |\/|
  105.  
  106.